# 访问限制
class Student1(object):
    def __init__(self, name, score):
        self.__name = name
        self.__score = score

    def print_score1(self):
        print('%s: %s' % (self.__name, self.__score))


bart = Student1('Daniel', 100)
# print(bart.__name)#外部无法访问实例变量__name
bart.print_score1()
print('========分界线========')


class Student(object):
    def __init__(self, name, score):
        self.__name = name
        self.__score = score

    def print_score(self):
        print('%s: %s' % (self.__name, self.__score))

    def get_name(self):
        return self.__name

    def get_score(self):
        return self.__score

    def set_score(self, score):
        if 0 <= score <= 100:
            self.__score = score
        else:
            raise ValueError('Error Score')


bart = Student('Daniel', 100)
print(bart.get_name())
print(bart.get_score())
bart.print_score()
bart.set_score(89)
bart.print_score()
# 变量名类似__xxx__的，也就是以双下划线开头，并且以双下划线结尾的，是特殊变量，特殊变量是可以直接访问的，不是private变量，所以，不能用__name__、__score__这样的变量名
# 双下划线开头的实例变量是不是一定不能从外部访问呢？其实也不是。不能直接访问__name是因为Python解释器对外把__name变量改成了_Student__name，所以，仍然可以通过_Student__name来访问__name变量：但是强烈建议你不要这么干
#练习：请把下面的Student对象的gender字段对外隐藏起来，用get_gender()和set_gender()代替，并检查参数有效性：
class Student2(object):
    def __init__(self, name, gender):
        self.__name = name
        self.__gender = gender
    def set_gender(self,gender):
        self.__gender=gender
    def get_gender(self):
        return self.__gender
# 测试:
bart1 = Student2('Bart', 'male')
if bart1.get_gender() != 'male':
    print('测试失败!')
else:
    bart1.set_gender('female')
    if bart1.get_gender() != 'female':
        print('测试失败!')
    else:
        print('测试成功!')